// index.js
const util = require('../../../util/util.js')
// const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    columns: [{
      key: '车辆类型',
      title: '车辆类型',
      width: 110,
      fixed: 'left',
      align: 'center'
    },
    {
      key: '出动数量',
      title: '出动数量',
      width: 100,
    }, {
      key: '总数量',
      width: 100,
      fixed: 'left',
      title: '总数量'
    }, {
      key: '出车率',
      width: 90,
      title: '出动率'
    }
    ],
    dataTable: [],
    timeStart: null, //时分秒，根据需要选择
    CLIENT_NO: '',
    clientNoIndex: 0,
    clientNoList: [],
  },
  onLoad: function () {
    var that = this;
    const currentDate = new Date();
    currentDate.setDate(currentDate.getDate() - 1);
    var CLIENT_NO = wx.getStorageSync("CLIENT_NO");
    that.setData({
      timeStart: util.formatDate(currentDate),
      CLIENT_NO: CLIENT_NO
    })

    this.getClientNoListInfo()
  },

  // 获取组织机构信息
  getClientNoListInfo() {
    var that = this;
    wx.request({
      url: 'https://www.ejingyou.cn/ServerHuanwei/CompanyapiBDEapi/Vehicle/GetClientInfo',
      data: {
        client_no: this.data.CLIENT_NO,
      },
      header: {
        'content-type': 'application/json' // 默认值     
      },
      success: (res) => {
        // 转换原始数据并初始化
        const originalData = res.data;
        const pickerData = originalData.map(item => ({
          value: item.CLIENT_GROUP_NO,
          label: item.CLIENT_GROUP_NAME
        }));
        const newItem = {value: "00000", label: "全市"};
        pickerData.unshift(newItem);
        this.setData({
          clientNoList:pickerData,
          CLIENT_NO: pickerData[0].value // 默认选中第一个
        });
        this.getCarRealtimeInfo(this.data.activeTab)
      }
    })
  },
  // 获取信息
  getCarRealtimeInfo() {
    var that = this;
    wx.request({
      url: 'https://www.ejingyou.cn/ServerHuanwei/CompanyapiBDEapi/Vehicle/GetVehicleWorkInfo',
      data: {
        clientNo: this.data.CLIENT_NO,
        date: this.data.timeStart,
      },
      header: {
        'content-type': 'application/json' // 默认值     
      },
      success: (res) => {
        // console.log(res.data)
        that.setData({
          dataTable: res.data.slice(1)
        });
        
        if(this.data.typeF) {
          this.setData({
            typeF: false
          })
        }

        if(this.data.typeC) {
          this.setData({
            typeC: false
          })
        }
      }
    })
  },
  onRowClick () {

  },
 
  openTypeF() {
    this.setData({
      typeF: true
    })
  },

  openTypeC() {
    console.log(this.data.clientNoList)
    this.setData({
      typeC: true
    })
  },

  selectDateStartChange(e) {
    this.setData({
      timeStart: e.detail.value
    })
  },

  selectClientNoChange(e) {
    const index = e.detail.value;
    const CLIENT_NO = this.data.clientNoList[index].value;
    this.setData({ 
      clientNoIndex: index,
      CLIENT_NO
    });
  },
  
  getDateTimeChange() {
    this.getCarRealtimeInfo(this.data.activeTab)
  },
})
